/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UserLogin.java
 *
 * Created on 2012-6-4, 21:24:07
 */

package icexam.user;

import icexam.ICExamView;
import icexam.dao.UserDao;
import icexam.util.CardIDChecker;
import java.text.ParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Ethan
 */
public class UserLogin extends javax.swing.JPanel {

    /** Creates new form UserLogin */
    public UserLogin() {
        initComponents();
    }

    private ICExamView mainView;
    public UserLogin(ICExamView aThis) {
        this();
        mainView = aThis;
        //for test
//        compNameT.setText("小泉剪刀");
//        ceoNameT.setText("飞哥");
//        iDT.setText("429006198208155118");
    }
    public void initRoleSelect(){
        jComboBox1.setSelectedIndex(0);
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        compNameT = new javax.swing.JTextField();
        jPanel6 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        ceoNameT = new javax.swing.JTextField();
        jPanel9 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        iDT = new javax.swing.JTextField();
        jPanel7 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jPanel8 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        loginB = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(600, 900));
        setMinimumSize(new java.awt.Dimension(350, 35));
        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(350, 200));
        setLayout(new java.awt.BorderLayout(5, 5));

        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(150, 200));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 150, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 341, Short.MAX_VALUE)
        );

        add(jPanel1, java.awt.BorderLayout.EAST);

        jPanel3.setName("jPanel3"); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 600, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        add(jPanel3, java.awt.BorderLayout.PAGE_START);

        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setPreferredSize(new java.awt.Dimension(150, 200));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 150, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 341, Short.MAX_VALUE)
        );

        add(jPanel2, java.awt.BorderLayout.WEST);

        jPanel4.setMaximumSize(new java.awt.Dimension(350, 32767));
        jPanel4.setMinimumSize(new java.awt.Dimension(300, 35));
        jPanel4.setName("jPanel4"); // NOI18N
        jPanel4.setPreferredSize(new java.awt.Dimension(330, 150));

        jPanel5.setMinimumSize(new java.awt.Dimension(300, 25));
        jPanel5.setName("jPanel5"); // NOI18N
        jPanel5.setPreferredSize(new java.awt.Dimension(300, 27));
        jPanel5.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(icexam.ICExamApp.class).getContext().getResourceMap(UserLogin.class);
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        jLabel2.setPreferredSize(new java.awt.Dimension(80, 15));
        jPanel5.add(jLabel2);

        compNameT.setText(resourceMap.getString("compNameT.text")); // NOI18N
        compNameT.setName("compNameT"); // NOI18N
        compNameT.setPreferredSize(new java.awt.Dimension(200, 21));
        jPanel5.add(compNameT);

        jPanel4.add(jPanel5);

        jPanel6.setMinimumSize(new java.awt.Dimension(300, 25));
        jPanel6.setName("jPanel6"); // NOI18N
        jPanel6.setPreferredSize(new java.awt.Dimension(300, 27));
        jPanel6.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N
        jLabel3.setPreferredSize(new java.awt.Dimension(80, 15));
        jPanel6.add(jLabel3);

        ceoNameT.setText(resourceMap.getString("ceoNameT.text")); // NOI18N
        ceoNameT.setName("ceoNameT"); // NOI18N
        ceoNameT.setPreferredSize(new java.awt.Dimension(200, 21));
        jPanel6.add(ceoNameT);

        jPanel4.add(jPanel6);

        jPanel9.setMinimumSize(new java.awt.Dimension(300, 25));
        jPanel9.setName("jPanel9"); // NOI18N
        jPanel9.setPreferredSize(new java.awt.Dimension(300, 27));
        jPanel9.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N
        jLabel6.setPreferredSize(new java.awt.Dimension(80, 15));
        jPanel9.add(jLabel6);

        iDT.setText(resourceMap.getString("iDT.text")); // NOI18N
        iDT.setName("iDT"); // NOI18N
        iDT.setPreferredSize(new java.awt.Dimension(200, 21));
        jPanel9.add(iDT);

        jPanel4.add(jPanel9);

        jPanel7.setMinimumSize(new java.awt.Dimension(300, 25));
        jPanel7.setName("jPanel7"); // NOI18N
        jPanel7.setPreferredSize(new java.awt.Dimension(300, 27));
        jPanel7.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N
        jLabel4.setPreferredSize(new java.awt.Dimension(80, 15));
        jPanel7.add(jLabel4);

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "考生", "管理员" }));
        jComboBox1.setName("jComboBox1"); // NOI18N
        jComboBox1.setPreferredSize(new java.awt.Dimension(200, 21));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });
        jPanel7.add(jComboBox1);

        jPanel4.add(jPanel7);

        jPanel8.setMinimumSize(new java.awt.Dimension(300, 25));
        jPanel8.setName("jPanel8"); // NOI18N
        jPanel8.setPreferredSize(new java.awt.Dimension(300, 27));
        jPanel8.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel5.setName("jLabel5"); // NOI18N
        jLabel5.setPreferredSize(new java.awt.Dimension(80, 15));
        jPanel8.add(jLabel5);

        loginB.setText(resourceMap.getString("loginB.text")); // NOI18N
        loginB.setName("loginB"); // NOI18N
        loginB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginBActionPerformed(evt);
            }
        });
        jPanel8.add(loginB);

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jPanel8.add(jButton2);

        jPanel4.add(jPanel8);

        add(jPanel4, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        if(jComboBox1.getSelectedIndex()==1){
            mainView.goToAdminLogin();
        }else{
            mainView.goToUserLogin();
        }
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void loginBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginBActionPerformed
   

        UserBean user = new UserBean();
        String temp = this.compNameT.getText();
        if(temp==null||temp.length()<2){
            javax.swing.JOptionPane.showMessageDialog(this, "请输入正确的字号。");
            return;
        }
        user.setCompName(temp);
        temp = this.ceoNameT.getText();
        String regex = "^[\u4e00-\u9fa5]{2,5}";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(temp);
        if(matcher.matches())
            user.setCeoName(temp);
        else{
            javax.swing.JOptionPane.showMessageDialog(this, "请输入正常的2-5个字的中文名字。");
            return;
        }
        temp = this.iDT.getText();
        String idError = null;
        try {
            idError = CardIDChecker.IDCardValidate(temp);
        } catch (ParseException ex) {
            javax.swing.JOptionPane.showMessageDialog(this, "请输入有效的身份证号码");
            return;
        }
        if(idError.length()!=0){
            javax.swing.JOptionPane.showMessageDialog(this, idError+" 请重新输入。");
            return;
        }
        user.setIdcard(temp);        
        user.setAdmin(false);
        UserDao dao = new UserDao();
        if(dao.companyErrorBoss(user)){
            javax.swing.JOptionPane.showMessageDialog(this, "数据库中存在此字号信息，但其负责人不是您输入的名字或身份证,\n如果您确定输入正确无误，请联系工作人员解决问题。");
            return;
        }else{
            if(!dao.haveThisUser(user)){
                dao.createUser(user);
            }
        }
        
        this.mainView.setUser(user);
        this.mainView.goToUserMain(new UserMainP(this.mainView));
    }//GEN-LAST:event_loginBActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField ceoNameT;
    private javax.swing.JTextField compNameT;
    private javax.swing.JTextField iDT;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JButton loginB;
    // End of variables declaration//GEN-END:variables

}
